package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "";
        return JDBC.getJdbc().update(sql);
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "UPDATE `product` SET ";
        if (!"".equals(productModel.getName())) {
            sql += "`name` = '" + productModel.getName() + "', ";
        }
        if (!BigDecimal.valueOf(0).equals(productModel.getNormalPrice())) {
            sql += "`normal_price` = '" + productModel.getNormalPrice() + "', ";
        }


        sql += "`price` = '" + productModel.getPrice() + "', " +
                "`content` = '1', " +
                "`is_show` = '1', " +
                "`is_recommend` = '1', " +
                "`img_href` = '1', " +
                "`create_time` = now(), " +
                "`remark` = '1', " +
                "`enable` = '" + productModel.getEnable() + "', " +
                "`where_show` = '2',";
        if (productModel.getCuisineId() != -1) {
            sql += " `cuisine_id` = '1',";
        }

        sql += " remark=''  WHERE " +
                "(`id` = '" + productModel.getId() + "');\n";
        return JDBC.getJdbc().update(sql);
    }

    @Override
    public int delete(ProductModel productModel) {
        return 0;
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        return null;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "select * from (\n" +
                "SELECT p.*,c.name cuisineName FROM product p\n" +
                "left join cuisine c\n" +
                "on p.cuisine_id=c.id) temp \n";
//                "where  name like '%2%'";
//        sql += getWhereSql(productModel);
        sql += " order by id desc ";
        int start = (productModel.getPage() - 1) * productModel.getLimit();
        sql += " limit " + start + "," + productModel.getLimit();
        return getList(JDBC.getJdbc().query(sql));
    }

    private List<ProductModel> getList(ResultSet query) {
        List<ProductModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                ProductModel model = new ProductModel();
                model.setId(query.getInt("id"));
                model.setName(query.getString("name"));
                model.setNormalPrice(query.getBigDecimal("normal_price"));
                model.setPrice(query.getBigDecimal("price"));
                model.setImgHref(query.getString("img_href"));
//                model.setIsShow(query.getInt("is_show"));
                model.setCuisineName(query.getString("cuisineName"));
                model.setContent(query.getString("content"));
                model.setCuisineId(query.getInt("cuisine_id"));
                model.setEnable(query.getInt("enable"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "select count(*) count from product";
        return JDBC.getJdbc().getCount(sql);
    }

    @Override
    public int updateEnable(ProductModel productModel) {
        return 0;
    }

    @Override
    public int enable(ProductModel productModel) {
//        String sql = "update product set enable =enable^1 where id=" + productModel.getId();
        String sql = "UPDATE `product` SET  enable=\n" +
                " case enable \n" +
                "\twhen 1 then 0  \n" +
                "    when 0   then 1 \n" +
                "    else 1\n" +
                "end  WHERE (`id` = '" + productModel.getId() + "');";
        return JDBC.getJdbc().update(sql);
    }
}
